package BigDecimal类编程题;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class SumAndAvg {
    public static void main(String[] args) {
        double[] arr = {0.1,0.2,2.1,3.2,5.56,7.21};
        BigDecimal[] bdArr = new BigDecimal[arr.length];
        BigDecimal result= new BigDecimal("0.000");
        for (int i = 0; i < arr.length; i++) {
            bdArr[i] = BigDecimal.valueOf(arr[i]);
        }
        for (int i = 0; i < bdArr.length; i++) {
            result = result.add(bdArr[i]);
        }
        System.out.println("总值: "+result);
        System.out.println("平均值: "+result.divide(BigDecimal.valueOf(arr.length),3, RoundingMode.HALF_UP));
    }
}
